﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace KC.Presentation.WorkTool
{
	public partial class Stopwatch : UserControl
	{
		public Stopwatch()
		{
			InitializeComponent();
		}

		public DateTime Started;
		public TimeSpan Duration;
		public bool isRunning = false,
			HasRun = false;


		private void btnStart_Click(object sender, EventArgs e)
		{
			if (isRunning) // Stop 
			{
				isRunning = false;
				UpdateTime();
				btnStart.Text = "&Start";
			}
			else // start 
			{
				if (!HasRun)
					Started = DateTime.Now;
				isRunning = true;
				HasRun = true;
				btnStart.Text = "&Stop";
			}

		}

		private void btnReset_Click(object sender, EventArgs e)
		{
			Started = DateTime.Now;
			txtTime.Text = "";
			if (!isRunning)
				HasRun = false;
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			UpdateTime();
		}


		private void UpdateTime()
		{
			if (!isRunning) return;

			Duration = DateTime.Now - Started;
			txtTime.Text =
				Duration.Days.ToString() + "d, " +
				Duration.Hours.ToString() + "h, " +
				Duration.Minutes.ToString() + "m, " +
				Duration.Seconds.ToString() + "." + Duration.Milliseconds.ToString() + "s";
		}

	}
}
